﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.Mvc;
using WebGioiThieu.Models;

namespace WebGioiThieu.Areas.Admin
{
    public class MvcHelper
    {
        private static List<Type> GetSubClasses<T>()
        {
            return Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(T))).ToList();
        }

        public static List<Type> GetControllerTypes()
        {
            List<Type> controllerNames = new List<Type>();
            GetSubClasses<Controller>().ForEach(
                type => controllerNames.Add(type));
            return controllerNames;
        }
        public static List<string> GetActionNames(Type controllerName)
        {
            return new ReflectedControllerDescriptor(controllerName)
                .GetCanonicalActions()
                .Where(a => a.GetCustomAttributes(typeof(CustomAuthorizeAttribute), false).Count()>0)
                .Select(a=>a.ActionName).Distinct().ToList();
        }
        public static string GetCurrentName(HttpContextBase httpContext)
        {
            var rd = httpContext.Request.RequestContext.RouteData;
            string area = "";
            string actionName = rd.GetRequiredString("action");
            string controllerName = rd.GetRequiredString("controller");
            try
            {
                area = rd.DataTokens["area"].ToString();
            }
            catch
            {
                area = "";
            }
            string name = (string.IsNullOrEmpty(area) ? "" : ("/" + area)) + "/" + controllerName + "/" + actionName;
            return name;
        }

        
    }
}